package com.faithbj.danaliverpool.web.controller.admin;

import java.util.HashSet;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.faithbj.common.configuration.Pager;
import com.faithbj.common.model.Region;
import com.faithbj.common.model.Roles;
import com.faithbj.common.model.User;
import com.faithbj.danaliverpool.service.RegionService;
import com.faithbj.danaliverpool.service.RoleService;
import com.faithbj.danaliverpool.service.UserService;

/**
 * @author jrn E-mail: rinanjin@gmail.com
 * @version 0.1 
 * 创建时间：2012-6-8 下午11:39:25
 * 类说明
 */
@Controller
@RequestMapping("/faith/user")
public class AdminUserController extends BaseAdminController
{
	private static final long serialVersionUID = 1L;

	@Resource
	private UserService userService;
	
	@Resource
	private RoleService roleService;
	
	@Resource
	private RegionService regionService;
	
	private static final String LIST = "/admin/user_list";
	
	private static final String INPUT = "/admin/user_input";
	
	private static final String DETAIL = "/admin/user_detail";
	
	private static final String OPERRATE_RETURN_URL = "user/list";
	
	@RequestMapping(value = "list")
	public String list(@ModelAttribute("pager") Pager pager, ModelMap map)
	{
		Pager pagerVal = userService.findByPager(pager);
		List<Roles> roleList = roleService.getAll();
		map.put("pager", pagerVal);
		map.put("roleList", roleList);
		return LIST;
	}
	
	@RequestMapping(value = "add")
	public String add(ModelMap map)
	{
		List<Roles> rolesList = roleService.getAll();
		List<Region> regionList = regionService.getAll();
		
		map.put("rolesList", rolesList);
		map.put("regionList", regionList);
		map.put("isAddAction", true);
		return INPUT;
	}
	
	@RequestMapping(value = "edit")
	public String edit(ModelMap map)
	{
		map.put("isAddAction", false);
		return INPUT;
	}
	
	@RequestMapping(value = "detail")
	public String detail(@RequestParam("id") String id, ModelMap map)
	{
		User user = userService.get(id);
		
		map.put("user", user);
		return DETAIL;
	}
	
	public String changePassword(@RequestParam("id") String id, @RequestParam("oldPassword") String oldPassword, @RequestParam("newPassword") String newPassword)
	{
		
		// TODO 此处只有用户自己能修改自己的用户名吗？
		return LIST;
	}
	
	public String detail(String id)
	{
		return DETAIL;
	}
	
	@RequestMapping(value = "save")
	public String save(@ModelAttribute("user") User user, HttpServletRequest request, ModelMap map)
	{
		// 获取设置用户角色
		List<Roles> rolesList = null;
		String[] ids = request.getParameterValues("ids");
		if(ids.length > 0)
		{
			rolesList = roleService.get(ids);
		}
		
		// 获取用户行政区域
		String regionId = user.getRegion().getId();
		Region region = null;
		if(user.getRegion() != null && StringUtils.hasText(regionId))
		{
			region = regionService.get(regionId);
		}
		
		user.setRoles(new HashSet<Roles>(rolesList));
		user.setRegion(region);
		user.setOperator(getLoginUser().getUsername());
		userService.save(user);
		userService.flush();
		map.put("redirectUrl", OPERRATE_RETURN_URL);
		return SUCCESS;
	}
	
	@RequestMapping(value = "update")
	public String update(@ModelAttribute("user") User user)
	{
		userService.update(user);
		userService.flush();
		return LIST;
	}
	
	@RequestMapping(value = "delete")
	public String delete(@RequestParam("id") String id, ModelMap map)
	{
		userService.delete(id);
		userService.flush();
		map.put("redirectUrl", OPERRATE_RETURN_URL);
		return SUCCESS;
	}

	public UserService getUserService()
	{
		return userService;
	}

	public void setUserService(UserService userService)
	{
		this.userService = userService;
	}
}
